package com.ntanh.mealplanner.main.objects;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;

import com.ntanh.mealplanner.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Anh Nguyen on 3/2/2015.
 */
public class MealType implements Comparable<MealType> {

    private Integer value;
    private String name;

    private static List<MealType> types;

    public MealType(Integer value, String name) {
        this.value = value;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getValue() {
        return value;
    }

    public void setValue(Integer value) {
        this.value = value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof MealType)) return false;

        MealType mealType = (MealType) o;

        if (value != mealType.value) return false;
        if (name != null ? !name.equals(mealType.name) : mealType.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = value;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        return result;
    }

    @Override
    public int compareTo(MealType another) {
        return this.value.compareTo(another.getValue());
    }

    public static List<MealType> loadFromConfigFile(Context context) {
        if(types != null){
            return types;
        }
        types = new ArrayList<MealType>();

        TypedArray mealTypes = context.getResources().obtainTypedArray(R.array.mealTypes);
        for (int i = 0; i < mealTypes.length(); i++) {
            types.add(new MealType(i, mealTypes.getString(i)));
        }

        return types;
    }

    public static MealType getFromName(Context context, String name) {
        if(types == null){
            loadFromConfigFile(context);
        }

        for(MealType type : types){
            if(type.getName().equals(name)){
                return type;
            }
        }

        return null;
    }


}
